#ifndef AUTOADMINMODULE_H
#define AUTOADMINMODULE_H

#include <QMainWindow>
#include <QSqlDatabase>
#include <QSqlRelationalTableModel>
#include <QSqlField>
#include <QSqlError>
#include <QSqlRecord>
#include <QTableView>
#include <QAction>
#include <QDebug>
#include <QHeaderView>
#include <QMessageBox>
#include <QGridLayout>
#include <QGroupBox>
#include <QFormLayout>
#include <QButtonGroup>
#include <QPushButton>
#include <QLineEdit>
#include <QDateEdit>
#include <QRadioButton>
#include <QPixmap>
#include <QLabel>
#include <QKeyEvent>
#include <QKeySequence>
#include <QShortcut>

#include "CustomToolbar.h"

class AutoAdminModule : public QMainWindow
{
    Q_OBJECT

public:
    explicit AutoAdminModule(QWidget *parent = 0);

signals:

public slots:
//    void ManageDomainsBtn();
    void UpdateClientList();
    void CorrectViewOfClients();
    void UpdateAutoadminsList();
    void CorrectViewOfAutoadmins();
    void UpdateListsBtn();
    void LimpiarListaAutoadmins();
    void SeleccionarCliente();
    void SeleccionarAutoadminClienteActual();

    // Filtros Rápidos
    void FindRazonSocial();
    void FilterRazonSocial();
    void FindAutoadmin();
    void FilterAutoadmin();
    // ABM
    void AltaBtn();
    void BajaBtn();
    void ModificarBtn();
    void GuardarBtn();
    void AutoadminViewClicked(const QModelIndex &index);
    void HabilitarGuardarBtn();
    void DeshabilitarABM();

private:
    CustomModulesToolbar *autoAdmin_toolbar;
    QAction *admin_autoAdmin;
    QAction *update_lists;
    // Filtros
    QRadioButton *find_razon_social;
    QRadioButton *find_seccion;
    QLineEdit *find_line;
    // Botones
    QPushButton *altaBtn;
    QPushButton *bajaBtn;
    QPushButton *modificarBtn;
    QPushButton *guardarBtn;
    // Dominio
    QLineEdit *line_seccion;
    QLineEdit *line_link;
    QLineEdit *line_usuario;
    QLineEdit *line_contrasenia;
    //
    QString cliente;
    QString idAutoAdmin;
    QString filtro_cliente;
    QString filtro_autoAdmin;

    bool on_add_autoAdmin;
    bool on_edit_autoAdmin;
    int row_edited;

    void UpdateListsToolBar();
};

class AutoAdminSqlTableModel : public QSqlRelationalTableModel
{
    Q_OBJECT
public:
    AutoAdminSqlTableModel(QObject * parent = 0, QSqlDatabase db = QSqlDatabase())
        : QSqlRelationalTableModel(parent,db) {;}
    QVariant data (const QModelIndex & index, int role = Qt::DisplayRole) const
    {
        // Alineación texto
        if (role == Qt::TextAlignmentRole) {
            int col = index.column();
            if (col == 3 || col == 4 || col == 5) {
                return QVariant (Qt::AlignVCenter | Qt::AlignHCenter);
            }
        }

        if (role == Qt::FontRole) {
            return QVariant (QFont("Helvetica57-Condensed"));
        }

        return QSqlRelationalTableModel::data(index,role);
    }
};

class CAutoAdminSqlTableModel : public QSqlRelationalTableModel
{
    Q_OBJECT
public:
    CAutoAdminSqlTableModel(QObject * parent = 0, QSqlDatabase db = QSqlDatabase())
        : QSqlRelationalTableModel(parent,db) {;}
    QVariant data (const QModelIndex & index, int role = Qt::DisplayRole) const
    {
        QSqlRelationalTableModel *autoAdmin_model = new QSqlRelationalTableModel();

        if(role == Qt::BackgroundColorRole)
        {
            if (index.column() == 1) {
                QModelIndex index2 = index.model()->index(index.row(), 1, index.parent()); // get index of "watched" column
                autoAdmin_model->setTable("autoadministrable");
                QSqlDatabase().transaction();
                if (!autoAdmin_model->select()) {
                    QSqlDatabase().rollback();
                }
                else {
                    QSqlDatabase().commit();
                    QString rSocial;
                    int rowCount = autoAdmin_model->rowCount();
                    for (int i = 0; i < rowCount; ++i) {
                        rSocial = autoAdmin_model->index(i, 1, index.parent()).data(Qt::DisplayRole).toString();
                        if (index2.data(Qt::DisplayRole).toString() == rSocial) {
                            return QVariant(QColor(Qt::green));
                        }
                    }
                }
            }
        }

        if (role == Qt::FontRole) {
            return QVariant (QFont("Helvetica57-Condensed"));
        }

        return QSqlRelationalTableModel::data(index,role);
    }
};

#endif // AUTOADMINMODULE_H
